package com.oigx.lookmu.ui;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.StatusBar;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.ui.content.Content;
import com.oigx.lookmu.core.SettingsState;
import com.oigx.lookmu.core.TxtBook;
import com.oigx.lookmu.core.TxtChapter;
import com.oigx.lookmu.core.TxtReaderUtil;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

public class LookDialog {

    private final Project project;

    private JPanel mainPanel;
    private JTabbedPane lookMu;
    private JBScrollPane fileListView;
    private JList<String> fileList;
    private JBScrollPane chapterListView;
    private JList<String> chapterList;


    public LookDialog(Project project) {
        this.project = project;
    }

    public JComponent getRootComponent() {
        this.initFileList();
        return mainPanel ;
    }

    public void initFileList() {
        SettingsState settingsState = SettingsState.getInstance();
        System.out.println(settingsState.filePath);
        if (settingsState.chapterList!=null){
            System.out.println(settingsState.chapterList.size());
        }
        System.out.println(settingsState.fileIndex+"---"+settingsState.chapterIndex+"---"+settingsState.readLine);
        boolean initChapterList = false;
        DefaultListModel<String> dataModule = new DefaultListModel<>();
        if (settingsState.bookList!=null){
            System.out.println("bookList size:"+settingsState.bookList.size());
            for (TxtBook txtBook : settingsState.bookList){
                dataModule.addElement(txtBook.getBookName());
            }
            fileList.setModel(dataModule);
            if (settingsState.fileIndex>=0 && settingsState.fileIndex<settingsState.bookList.size()){
                TxtBook txtBook = settingsState.bookList.get(settingsState.fileIndex);
                settingsState.chapterList = TxtReaderUtil.readFile(txtBook.getFilePath(), txtBook.getCharsetName());
                initChapterList(settingsState.chapterList);
                initChapterList = true;
            }
        }
        if (!initChapterList){
            DefaultListModel<String> chapterModule = new DefaultListModel<>();
            chapterList.setModel(chapterModule);
        }
        fileList.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount()==2){
                    System.out.println("File List Selected " + fileList.getSelectedValue());
                    StatusBar statusBar = WindowManager.getInstance().getStatusBar(project);
                    if (statusBar != null) {
                        statusBar.setInfo("select book:" + fileList.getSelectedValue());
                    }
                    TxtBook txtBook = settingsState.bookList.get(fileList.getSelectedIndex());
                    System.out.println(txtBook.getBookName());
                    settingsState.fileIndex = fileList.getSelectedIndex();
                    settingsState.chapterList = TxtReaderUtil.readFile(txtBook.getFilePath(), txtBook.getCharsetName());
                    settingsState.chapterIndex = 0;
                    settingsState.readLine = 0;
                    settingsState.readLineList = null;

                    lookMu.setSelectedIndex(1);
                    initChapterList(settingsState.chapterList);
                }
            }
            @Override
            public void mousePressed(MouseEvent e) {

            }
            @Override
            public void mouseReleased(MouseEvent e) {

            }
            @Override
            public void mouseEntered(MouseEvent e) {

            }
            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
        System.out.println("init select index="+lookMu.getSelectedIndex());
        lookMu.addChangeListener(e -> {
            int sl = lookMu.getSelectedIndex();
            System.out.println("select index="+sl);
            // 获取到对应的 toolWindows，可以进行相关操作
            ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow("LookMu");
            if (toolWindow!=null){
                Content content = toolWindow.getContentManager().getSelectedContent();
                if (content!=null){
                    if (sl==0){
                        content.setDisplayName("书架");
                    } else {
                        if (settingsState.bookList!=null && settingsState.fileIndex>=0 && settingsState.fileIndex<settingsState.bookList.size()) {
                            TxtBook txtBook = settingsState.bookList.get(settingsState.fileIndex);
                            content.setDisplayName(txtBook.getBookName());
                        } else {
                            content.setDisplayName("章节");
                        }
                    }
                }
            }
        });
    }

    private void initChapterList(List<TxtChapter> txtChapterList) {
        DefaultListModel<String> dataModule = new DefaultListModel<>();
        for (TxtChapter txtChapter : txtChapterList) {
            dataModule.addElement(txtChapter.getTitleName());
        }
        chapterList.setModel(dataModule);
        chapterList.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount()==2){
                    System.out.println("TxtChapter List Selected " + chapterList.getSelectedValue());
                    SettingsState settingsState = SettingsState.getInstance();
                    settingsState.chapterIndex = chapterList.getSelectedIndex();
                    settingsState.readLine = 0;
                    settingsState.readLineList = null;
                    StatusBar statusBar = WindowManager.getInstance().getStatusBar(project);
                    if (statusBar != null) {
                        statusBar.setInfo("" + chapterList.getSelectedValue());
                    }
                }
            }
            @Override
            public void mousePressed(MouseEvent e) {

            }
            @Override
            public void mouseReleased(MouseEvent e) {

            }
            @Override
            public void mouseEntered(MouseEvent e) {

            }
            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
    }

}
